package com.jrmf.redenvelopes.order.job;

import com.jrmf.redenvelopes.order.common.ApplicationContextProvider;
import com.jrmf.redenvelopes.order.dao.AccountCheckResultMapper;
import com.jrmf.redenvelopes.order.dao.JdpaySignPayMapper;
import com.jrmf.redenvelopes.order.model.AccountCheckResult;
import com.jrmf.redenvelopes.order.util.DateUtil;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

public class AccountCheckCallable implements Callable<Boolean> {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private  String day;
    private  String platformType;

    public AccountCheckCallable() {
    }

    public AccountCheckCallable(String day,String platformType) {
        this.day = day;
        this.platformType =platformType;
    }

    public String getPlatformType() {
        return platformType;
    }

    public void setPlatformType(String platformType) {
        this.platformType = platformType;
    }

    public String getDay() {
        return day;
    }

    public void setDay(String day) {
        this.day = day;
    }


    @Override
    public Boolean call() throws Exception {
        //获取依赖 AccountCheckResultMapper
        Boolean flag =false;
        JdpaySignPayMapper jdpaySignPayMapper = ApplicationContextProvider.getBean("jdpaySignPayMapper");
        AccountCheckResultMapper accountCheckResultMapper = ApplicationContextProvider.getBean("accountCheckResultMapper");

        //获取任务参数
        String contrastDate = this.getDay();
        String platformType = this.getPlatformType();
        if (null!= contrastDate && !"".equals(contrastDate) && null!= platformType && !"".equals(platformType)){
            logger.info("开始对账");
            //执行对账
            Map<String ,Object> param =new HashMap<>();

            param.put("dateTime",contrastDate);
            param.put("thirdpartyPaytype",platformType);
            List<Map<String,Object>> list1 =jdpaySignPayMapper.accountThird2Mf2(param);//短款
            System.out.println(list1.size());
            List<Map<String,Object>> list2 =jdpaySignPayMapper.accountMf2Third2(param);//长款
            System.out.println(list2.size());
            List<Map<String,Object>> list3 =jdpaySignPayMapper.accountThirdwithMf2(param);//双方都有 但是本地不是成功状态
            System.out.println(list3.size());


            List<Map<String,Object>> list4 =jdpaySignPayMapper.accountThirdwithMfAlign2(param);//时间戳内 对正
            System.out.println(list4.size());

            /*String preDay = DateUtil.getSpecifiedDayBefore(contrastDate,"yyyyMMdd");
            String aftDay =DateUtil.getSpecifiedDayAfter(contrastDate,"yyyyMMdd");
            List<String> dayList  =new ArrayList<>();
            dayList.add(preDay);
            dayList.add(aftDay);
            param.put("list",dayList);*/
            List<Map<String,Object>> list5 =jdpaySignPayMapper.accountThirdwithMf3(param);//时间戳外 对不齐 归属到3中
            System.out.println(list5.size());
            List<Map<String,Object>> list6 =jdpaySignPayMapper.accountThirdwithMf4(param);//双方都有 但是本地不是成功状态
            System.out.println(list6.size());
            List<Map<String,Object>> finalList =new ArrayList<>();
            if (list1.size()>0){
                finalList.addAll(list1);
            }
            if (list2.size()>0){
                finalList.addAll(list2);
            }
            if (list3.size()>0){
                finalList.addAll(list3);
            }
            if (list4.size()>0){
                finalList.addAll(list4);
            }
            if (list5.size()>0){
                finalList.addAll(list5);
            }
            if (list6.size()>0){
                finalList.addAll(list6);
            }
            int sourceCount =finalList.size();


            if (sourceCount>0){ //有待插入数据
                if (sourceCount>1024){
                    //分批次插入
                    int pageSize =0;
                    if(sourceCount%1024==0){
                        pageSize=sourceCount/2014;
                    }
                    else{
                        pageSize=(sourceCount/1024)+1;
                    }
                    int subSuccessCount =0;
                    for(int i=1;i<=pageSize;i++){
                        //创建新的
                        if(i==pageSize){
                            //从 (i-1)*1024--totalNum
                            List<Map<String,Object>> subList= finalList.subList((i-1)*1024,sourceCount);
                            //System.out.println(subList);
                            int r =accountCheckResultMapper.batchInsert(subList);
                            if (r==(sourceCount-(i-1)*1024)){
                                subSuccessCount=subSuccessCount+r;
                            }
                            else {
                                break;
                            }

                        }
                        else{
                            //从(i-1)*1024--i*1024
                            List<Map<String,Object>> subList= finalList.subList((i-1)*1024,1024*(i));
                            int r=accountCheckResultMapper.batchInsert(subList);
                            if (r==1024){
                                subSuccessCount =subSuccessCount+r;
                            }
                            else{
                                break;
                            }
                        }
                    }
                    if (subSuccessCount ==sourceCount){
                        flag =true;
                        logger.info("对账差错数据持久化成功！");
                    }
                    else{
                        logger.info("对账差错数据持久化失败！");
                    }
                }
                else{
                    //单批次直接插入
                    int result =accountCheckResultMapper.batchInsert(finalList);
                    if (result ==sourceCount){//判断是否插入无误
                        logger.info("对账差错数据持久化成功！");
                        flag =true;
                    }
                    else{
                        logger.info("对账差错数据持久化失败！");
                    }
                }
            }

            else {
                logger.info(contrastDate+"全部对齐");
            }

           /* if (fCount>0){
                int result =accountCheckResultMapper.batchInsert(finalList);
                if (result==finalList.size()){
                    logger.info("对账差错数据持久化成功！");
                }
            }*/


        }
        //持久化差错结果和对账log
        // 全部对齐 / 每种差错类型各自多少条 todo
        return flag;
    }
}
